package spring.cloud.tasks.executor.executor.task.task_trigger;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.common.nodepaths.ExecutorNodePaths;
import spring.cloud.tasks.common.nodepaths.ExecutorTaskNodePath;
import spring.cloud.tasks.common.nodepaths.TaskNodePaths;
import spring.cloud.tasks.executor.executor.task.Service;
import spring.cloud.tasks.executor.executor.task.TaskExecutor;

@Slf4j
@Setter
@Getter
public class TaskExecutorInfoService extends Service {


    public TaskExecutorInfoService(final TaskExecutor taskExecutor) {
        super(taskExecutor);
    }

    @Override
    public void start() {

    }

    public void persistExecutorOnline() {
        curator.ephemeralCreate(TaskNodePaths.getTaskExecutorNodePath(taskId, executorId), "");
        curator.ephemeralCreate(ExecutorTaskNodePath.getExecutorTaskNode(executorId, taskId), "");
    }


    public void resetCount() {

        persistProcessSuccessCount(0);
        persistProcessFailureCount(0);

    }


    /**
     * 持久化统计处理数据成功的数量的数据.
     *
     * @param processSuccessCount 成功数
     */
    public void persistProcessSuccessCount(final int processSuccessCount) {
        String nodePath = TaskNodePaths.getTaskNodePath(taskId, ExecutorNodePaths.getProcessSuccessCountNode(executorId));
        this.curator.merge(nodePath, String.valueOf(processSuccessCount));
    }

    /**
     * 持久化统计处理数据失败的数量的数据.
     *
     * @param processFailureCount 失败数
     */
    public void persistProcessFailureCount(final int processFailureCount) {
        String nodePath = TaskNodePaths.getTaskNodePath(taskId, ExecutorNodePaths.getProcessFailureCountNode(executorId));
        this.curator.merge(nodePath, String.valueOf(processFailureCount));
    }


}
